package com.open.capacity.nk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.open.capacity.commons.Result;
import com.open.capacity.finance.entity.AuditModel;
import com.open.capacity.nk.service.NkDirectpaymentreceiptService;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import javafx.util.Pair;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.nk.entity.AaDepartment;
import com.open.capacity.nk.service.AaDepartmentService;

import com.open.capacity.commons.PageResult;

/**
 * @author
 * @email
 * @date 2023-03-01 22:09:01
 */
@RestController
@RequestMapping("aadepartments")
@Api(tags = "")
public class AaDepartmentController {

    @Autowired
    private AaDepartmentService aaDepartmentService;

    @Autowired
    private NkDirectpaymentreceiptService directpaymentreceiptService;

    /**
     * 单位列表
     */
    @RequestMapping("/listOrganization")
    @PreAuthorize("hasAnyAuthority('aa_department:aadepartment:list')")
    public Result<List<TreeVo>> listOrganization(@RequestParam(value = "parentId", defaultValue = "0") String parentId) {
        Map<String, Object> params = new HashMap<>();
        List<TreeVo> orgList = directpaymentreceiptService.findOrganization(params);
        List<TreeVo> dropdownList = new ArrayList<>();
        orgList.forEach(m -> {
            if (StringUtils.isNotEmpty(m.getText())) {
                TreeVo node = new TreeVo();
                node.setId(m.getId());
                node.setText(m.getText());
                node.setTitle(m.getTitle());
                node.setValue(m.getId());
                node.setShowcheck(false);
                node.setCheckstate(0);
                node.setIsexpand(true);
                node.setComplete(false);
                node.setParentId("");
                dropdownList.add(node);
            }
        });

        return Result.succeed(dropdownList);
    }

    /**
     * 部门列表
     */
    @RequestMapping("/listDepartment")
    @PreAuthorize("hasAnyAuthority('aa_department:aadepartment:list')")
    public Result<List<TreeVo>> listDepartment(@RequestParam(value = "companyId") String companyId, @RequestParam(value = "parentId", defaultValue = "0") String parentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("companyId", companyId);
        List<AaDepartment> list = aaDepartmentService.findAll(params);
        List<TreeVo> dropdownList = new TreeList();
        list.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getName());
            node.setTitle(m.getName());
            node.setValue(m.getId());
            node.setShowcheck(false);
            node.setCheckstate(0);
            node.setIsexpand(true);
            node.setComplete(false);
            node.setParentId("");
            dropdownList.add(node);
        });

        return Result.succeed(dropdownList);
    }

}
